package com.alks.function.service.serviceImpl.finance.fincons;


import com.alks.common.aop.AutoPageAop;
import com.alks.common.data.thread.UserIdThread;
import com.alks.common.utils.DateUtils;
import com.alks.common.utils.beanutils.ObjectUtils;
import com.alks.common.utils.excelUtils.ExcelUtils;
import com.alks.entity.data.response.ResponseInfo;
import com.alks.function.data.dto.finance.fincons.CostDetailsDto;
import com.alks.function.data.dto.finance.fincons.CountDto;
import com.alks.function.data.request.finance.fincons.CostDetailsRequest;
import com.alks.function.mapper.finance.fincons.CostSttlementMapper;
import com.alks.function.service.finance.fincons.CostSttlementService;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class CostSttlementServiceImpl implements CostSttlementService {

    @Autowired
    private CostSttlementMapper costSttlementMapper;

    /**
     * 费用明细查询
     * @param request
     * @return
     */
    @Override
    @AutoPageAop
    public ResponseInfo getCostDetailsList(CostDetailsRequest request) {
        String companyId = UserIdThread.get().getCompanyId();
        //史倩待完成 应付标识未知
        //全部条数：25589    数据库：25592
        //未结算355
        //已结算25204

        List<CostDetailsDto> costDetailsList = costSttlementMapper.getCostDetailsList(companyId,request);

        PageInfo list = new PageInfo(costDetailsList);
        HashMap map = new HashMap<>();
        map.put("inboundDetailsList", costDetailsList);
        map.put("total", list.getTotal());
        return ResponseInfo.ok(map);
    }

    /**
     * 数量统计
     * @return
     */
    @Override
    public ResponseInfo getCount(List<Integer> ids) {
        String companyId = UserIdThread.get().getCompanyId();
        List<CostDetailsDto> costDetailsList = costSttlementMapper.getCostDetailsListById(ids);

        BigDecimal qty = BigDecimal.ZERO;
        BigDecimal amt = BigDecimal.ZERO;
        if(ObjectUtils.isNotEmpty(costDetailsList)){
            for (CostDetailsDto dto : costDetailsList) {
                qty = dto.getQty() != null ? qty.add(dto.getQty()) : qty.add(BigDecimal.ZERO);
                amt = dto.getAmt() != null ? amt.add(dto.getAmt()) : amt.add(BigDecimal.ZERO);
            }
        }

        CountDto countDto = new CountDto();
        countDto.setQty(qty);
        countDto.setAmt(amt);

        return ResponseInfo.ok(countDto);
    }

    /**
     * 删除费用明细
     * @param id
     * @return
     */
    @Override
    public ResponseInfo deleteCostDetailsById(Integer id) {
        List<CostDetailsDto> costDetailsList = costSttlementMapper.getCostDetailsListById(Collections.singletonList(id));
        if(ObjectUtils.isNotEmpty(costDetailsList)){
            String cfmFlag = costDetailsList.get(0).getCfmFlag();
            if(cfmFlag != null && cfmFlag.equals("Y")) {
                return ResponseInfo.error("已结算，不能删除");
            }
        }
        //删除
        int count = costSttlementMapper.deleteCostDetailsById(id);
        if(count > 0){
            return ResponseInfo.ok("删除成功");
        }else{
            return ResponseInfo.error("删除失败");
        }
    }

    /**
     * 导出
     * @param ids
     * @param response
     */
    @Override
    public void exportCostDetails(List<Integer> ids, HttpServletResponse response) throws IOException {
        List<CostDetailsDto> list = costSttlementMapper.getCostDetailsListById(ids);
        BigDecimal count = BigDecimal.ZERO;
        if(ObjectUtils.isNotEmpty(list)){
            for (CostDetailsDto dto : list) {
                count = dto.getAmt() != null ? count.add(dto.getAmt()) : count.add(BigDecimal.ZERO);
            }
        }
        Map<String, Object> map = new HashMap<>();
        map.put("count",count);
        ExcelUtils.creatByTemplate(response,"费用明细模板.xls","费用明细_"+DateUtils.dateTimeNow(),list,map);
    }

}
